package com.lq.util.jdbc;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

/**
 * 操作properties<br>
 * @author 吃人的肉
 * QQ:376870344<br>
 * email:liuqingrou@163.com
 */
public class LqConfig implements java.io.Serializable {

	private Properties props;

	private String propertiesName = "lqjdbc.properties";

	public LqConfig() {
		props = new Properties();
		init();
	}

	public LqConfig(String properName) {
		props = new Properties();
		init(properName);
	}

	public void init() {
		init(propertiesName);
	}

	
	
	public void init(String properName) {
		this.propertiesName = properName;
		String filePath = classPath() + propertiesName;
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(filePath));
			props.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public String classPath() {
		String basepath = Thread.currentThread().getContextClassLoader().getResource("").toString();
		// System.out.println("---classpath----"+basepath.substring(6,basepath.length()));
		if (System.getProperty("file.separator").equals("\\")) {
			return basepath.substring(6, basepath.length());
		} else {
			return basepath.substring(5, basepath.length());
		}

	}
	

	public String getString(String key) {
		try {
			String value = "";
			if (props.getProperty(key) != null) {
				value = new String(props.getProperty(key)
						.getBytes("ISO-8859-1"), "utf-8");
				;
			}
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	public Set<String> getPropNames() {

		Set<String> propNameSet = props.stringPropertyNames();

		return propNameSet;
	}

	public void setString(String key, String value) {
		if (!key.equals("")) {
			props.setProperty(key, value);
		}
	}

	public void setString(Map map) {
		Set<Entry<String, String>> entrySet = null;
		if (map != null && map.size() > 0) {
			entrySet = map.entrySet();
			Iterator<Entry<String, String>> iterator = entrySet.iterator();
			while (iterator.hasNext()) {
				Entry<String, String> entry = iterator.next();
				String key = entry.getKey();
				String value = entry.getValue();
				props.setProperty(key, value);
			}
		}

	}

	
	
	
	public void saveConfig(String comments) {
		String filePath = classPath() + propertiesName;
		OutputStream out = null;

		try {
			out = new FileOutputStream(filePath);
			props.store(out, comments);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	public static void main(String[] args) {
		System.out.println(LqConfig.class.getResource("/").getPath());
		System.out.println(System.getProperty("file.separator"));

	}

}
